package android.storefront.common.utils.sound;

import java.util.HashMap;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.storefront.activity.R;

public class SoundPoolUtils {

	public static final int SOUND_BUTTON_CLICK = 1;

	private SoundPool soundPool;
	private HashMap<Integer, Integer> soundPoolMap;
	private AudioManager audioManager = null;
	private Context context = null;
	private int streamVolume = 0;

	private static SoundPoolUtils poolUtils = null;

	public static SoundPoolUtils getInstance(Context context) {
		if (null == poolUtils) {
			poolUtils = new SoundPoolUtils();
			poolUtils.initSounds(context);
			return poolUtils;
		} else {
			return poolUtils;
		}
	}

	private void initSounds(Context context) {
		this.context = context;
		soundPool = new SoundPool(4, AudioManager.STREAM_RING, 0);
		addSound();
		
		audioManager = (AudioManager) this.context.getSystemService(Context.AUDIO_SERVICE);
		streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
	}

	public void playSound(int soundNum) {
		soundPool.play(soundPoolMap.get(soundNum), streamVolume, streamVolume, 1, 0, 1.0f);
	}
	
	//添加声音
	private void addSound() {
		soundPoolMap = new HashMap<Integer, Integer>();
		soundPoolMap.clear();
		soundPoolMap.put(SOUND_BUTTON_CLICK, soundPool.load(context, R.raw.click, 1));
	}

}
